package com.wehotel.common.handler;



import com.wehotel.common.exception.BizException;
import com.wehotel.common.model.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ValidationException;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public R jsonErrorHandler(HttpServletRequest request,BizException e) throws Exception{
        logger.error("处理出错，全局异常",e);
        R r = new R(e.getStatus(), e.getMessage());
        return r;
    }

    @ExceptionHandler(value = IllegalStateException.class)
    @ResponseBody
    public R jsonErrorHandler2(HttpServletRequest request,IllegalStateException e) throws Exception{
        logger.error("处理出错，全局异常",e);
        R r = new R(R.STATE_ERROR.getCode(), e.getMessage());
        return r;
    }

    @ExceptionHandler(value={IllegalArgumentException.class, ServletRequestBindingException.class, ValidationException.class})
    @ResponseBody
    public R jsonErrorHandler3(HttpServletRequest request,Exception e){
        logger.error("处理出错，全局异常",e);
        R r = new R(R.PARAM_ERROR.getCode(), e.getMessage());
        return r;
    }



    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R defaulErrorHandler(HttpServletRequest request, Exception e) throws  Exception{
        logger.error("处理出错，全局异常",e);

        return R.UNKNOW;
    }




}

